package cifrasong.usuario.persistencia;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cifrasong.database.CifraSongSQLiteHelper;
import cifrasong.database.DAO;
import cifrasong.usuario.dominio.Session;
import cifrasong.usuario.dominio.Usuario;
import cifrasong.usuario.negocio.UsuarioService;


public class UsuarioDAO extends DAO{

    private static final UsuarioDAO instance = new UsuarioDAO();

    private UsuarioDAO() {
        super();
    }
	
    public static UsuarioDAO getInstance(){
        return instance;
    }


    public void adicionarUsuario(Usuario usuario) {
        CifraSongSQLiteHelper database = getDataBaseHelper();
        open();
        ContentValues initialValues = new ContentValues();
        initialValues.put(database.getLogin(), usuario.getLogin());
        initialValues.put(database.getSenha(), usuario.getSenha());
        initialValues.put(database.getEmail(), usuario.getEmail());
        initialValues.put(database.getAtivo(), UsuarioService.passBoolean(usuario));
        getDatabase().insert(database.getTableNameUsuario(), null, initialValues);
        close();
    }

    public boolean login(String login, String senha){
        boolean condition = false;
        open();
        Cursor mCursor = getDatabase().rawQuery("SELECT * FROM " + getDataBaseHelper().getTableNameUsuario() + " WHERE login=? AND senha=? AND ativo = ? ", new String[]{login,senha,"1"});
        if (mCursor != null) {
            if (mCursor.getCount() > 0) {
                condition = true;
            }
        }
        close();
        return condition;
    }

    public boolean existeUsuario(Usuario usuario){
        boolean condition = false;
        open();
        Cursor mCursor = getDatabase().rawQuery("SELECT * FROM " + getDataBaseHelper().getTableNameUsuario() + " WHERE login=?", new String[]{(usuario.getLogin())});
        if (mCursor != null) {
            if (mCursor.getCount() > 0) {
                condition = true;
            }
        }
        close();
        return condition;
    }

    public boolean deleteUsuario(Usuario usuario) {
        open();
        getDatabase().execSQL("UPDATE  " + getDataBaseHelper().getTableNameUsuario() + " SET ativo = 0 WHERE ativo = 1 AND  login = ?", new String[]{usuario.getLogin()});
        close();
        return true;
    }

    public boolean updateUsuario(String email,String senha){
        open();
        getDatabase().execSQL("UPDATE  " + getDataBaseHelper().getTableNameUsuario() + " SET " + getDataBaseHelper().getEmail() + " = ? ," + getDataBaseHelper().getSenha() + " = ? WHERE login = ?", new String[]{email, senha, Session.usuarioLogado.getLogin()});
        close();
        return true;
    }

    public boolean setSession(String login){
        boolean onSession = false;
        open();
        Cursor mCursor = getDatabase().rawQuery("SELECT * FROM " + getDataBaseHelper().getTableNameUsuario() + " WHERE login=?", new String[]{(login)});
        if(mCursor != null){
            onSession = true;
            mCursor.moveToFirst();
            Session.usuarioLogado.setID(mCursor.getInt(mCursor.getColumnIndex(getDataBaseHelper().getID())));
            Session.usuarioLogado.setLogin(mCursor.getString(mCursor.getColumnIndex(getDataBaseHelper().getLogin())));
            Session.usuarioLogado.setEmail(mCursor.getString(mCursor.getColumnIndex(getDataBaseHelper().getEmail())));
            Session.usuarioLogado.setSenha(mCursor.getString(mCursor.getColumnIndex(getDataBaseHelper().getSenha())));
        }
        close();
        return onSession;
    }
}